package com.shangxian.giveithome.service.impl;


import com.shangxian.giveithome.code.CommonCode;
import com.shangxian.giveithome.code.MechanismCode;
import com.shangxian.giveithome.controller.UserFeiController;
import com.shangxian.giveithome.entity.*;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.mapper.ActivityMapper;
import com.shangxian.giveithome.mapper.MechanismDataMapper;
import com.shangxian.giveithome.mapper.VolunteerMapper;
import com.shangxian.giveithome.service.VolunteerService;
import com.shangxian.giveithome.utils.IGenerator;
import com.shangxian.giveithome.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.shangxian.giveithome.utils.DateTime.YMD;
import static com.shangxian.giveithome.utils.DateTime.getTime;


@Service
public class VolunteerServiceImpl implements VolunteerService {


    @Resource
    private VolunteerMapper volunteerMapper;

    @Resource
    private ActivityMapper activityMapper;

    @Autowired
    private IGenerator iGenerator;

    @Resource
    private MechanismDataMapper mechanismDataMapper;

    @Resource
    private UserFeiController userFeiController;

    /**
     * 根据机构id【简单】查询机构义工审核数据
     *
     * @param mr_id
     * @return
     */
    @Override
    public List<VolunteerExamine> queryById(Long mr_id) {
        List<VolunteerExamine> volunteers = volunteerMapper.queryStatusById(mr_id);
        if (CollectionUtils.isEmpty(volunteers)) {
            return new ArrayList<>();
        }
        return volunteers;
    }

    /**
     * 根据机构 义工列表 并且查出是否有新的审核数据
     *
     * @param sps
     * @param aLong
     * @param pageable
     * @return
     */
    @Override
    public PageableDetails queryVolunteerListAndToExamine(List<String> sps, Long aLong, Pageable pageable) {
        //当前页
        Integer pageNum = pageable.getPageNumber();
        //每页条数
        Integer pages = pageable.getPageSize();
        if (CollectionUtils.isEmpty(sps)) {
            sps = null;
        }
        Integer total = volunteerMapper.countStatusByVolunteer(sps, aLong);
        if (total == 0) {
            VolunteerReq volunteerReq = new VolunteerReq();
            Integer volunteerStatus0 = volunteerMapper.countStatus0(aLong);
            volunteerReq.setListExamineList(null);
            volunteerReq.setStatus0(volunteerStatus0);
            return new PageableDetails<>(0, 0, 0, 0, volunteerReq);
        }
        List<VolunteerListExamine> listExamines = volunteerMapper.queryStatusByVolunteer(sps, aLong, pageNum * pages, pages);
        Integer volunteerStatus0 = volunteerMapper.countStatus0(aLong);
        int totalPages = PageUtils.returnPages(total, pages);
        
        VolunteerReq volunteerReq = new VolunteerReq();
        volunteerReq.setListExamineList(listExamines);
        volunteerReq.setStatus0(volunteerStatus0);
        return new PageableDetails<>(pageNum, pages, total, totalPages, volunteerReq);
    }

    /**
     * 查询义工完整审核列表
     *
     * @param aLong
     * @param pageable
     * @return
     */
    @Override
    public PageableDetails queryVolunteerListToExamine(Long aLong, Pageable pageable) {
        //当前页
        Integer pageNum = pageable.getPageNumber();
        //每页条数
        Integer pages = pageable.getPageSize();
        Integer total = volunteerMapper.countStatus0(aLong);
        if (total == 0) {
            ExceptionUtils.error(CommonCode.DATA_ERROR);
        }
        List<VolunteerListExamine> listExamines = volunteerMapper.queryStatusVolunteer(aLong, pageNum * pages, pages);
        int totalPages = 0;
        if (total % pageable.getPageSize() != 0) {
            totalPages += 1;
        }
        totalPages += total / pageable.getPageSize();
        mechanismDataMapper.cleanVolunUntreated(aLong);
        return new PageableDetails<>(pageNum, pages, total, totalPages, listExamines);
    }

    /**
     * 查询义工详情
     *
     * @param aLong
     * @param id
     * @return
     */
    @Override
    public VolunteerDetails queryVolunteerDetails(Long aLong, Long id) {
        Volunteer volunteer = volunteerMapper.queryDetails(id);
        if (volunteer == null) {
            ExceptionUtils.error(MechanismCode.VOLUNTEER_NOT_DATA);
        }
        if (!aLong.equals(volunteer.getMeId())) {
            ExceptionUtils.error(MechanismCode.USER_NOT_AUTH);
        }
        VolunteerDetails volunteerDetails = iGenerator.convert(volunteer, VolunteerDetails.class);
        String[] split = volunteer.getSpeciality().split(",");
        List<String> list = Arrays.asList(split);
        List<String> volunteerSps = volunteerMapper.querySpecialityDetails(list);
        volunteerDetails.setSpecialitys(volunteerSps);
        Integer activityNumber = activityMapper.queryCountActivityNumber(volunteer.getUserId());
        if (activityNumber == null) {
            activityNumber = 0;
        }
        volunteerDetails.setActivityNumber(activityNumber);
        return volunteerDetails;
    }

    /**
     * 查看义工审核详情
     *
     * @param aLong
     * @param id
     * @return
     */
    @Override
    public VolunteerExamineDetails queryVolunteerExamineDetails(Long aLong, Long id) {
        Volunteer volunteer = volunteerMapper.queryExamineDetails(id);
        if (volunteer == null) {
            ExceptionUtils.error(CommonCode.DATA_ERRORS);
        }
        if (!aLong.equals(volunteer.getMeId())) {
            ExceptionUtils.error(MechanismCode.USER_NOT_AUTH);
        }
        VolunteerExamineDetails volunteerExamineDetails = iGenerator.convert(volunteer, VolunteerExamineDetails.class);
        String[] split = volunteer.getSpeciality().split(",");
        List<String> list = Arrays.asList(split);
        List<String> volunteerSps = volunteerMapper.querySpecialityDetails(list);
        volunteerExamineDetails.setSpecialitys(volunteerSps);
        return volunteerExamineDetails;
    }

    /**
     * 义工申请
     * @param volunteerCreate
     */
    @Override
    @Transactional
    public void create(VolunteerCreate volunteerCreate) {
        Integer integer = volunteerMapper.queryExistence(volunteerCreate.getMe_id(), volunteerCreate.getUs_id());
        if (integer!=0){
            ExceptionUtils.error(MechanismCode.USER_NOT_VOLUNTEER);
        }
        volunteerCreate.setCreateTime(getTime(YMD));
        volunteerMapper.createVolun(volunteerCreate);
    }

    /**
     * 删除义工
     * @param aLong
     * @param id
     */
    @Override
    @Transactional
    public void deleteVolu(Long aLong, Long id) {
        Volunteer volunteer = volunteerMapper.queryDetails(id);
        if (volunteer==null){
            ExceptionUtils.error(CommonCode.DATA_ERRORS);
        }
        if (!volunteer.getMeId().equals(aLong)) {
           ExceptionUtils.error(MechanismCode.USER_NOT_D_AUTH);
        }
        volunteerMapper.delete(aLong,id);
        mechanismDataMapper.updateDe(MechanismDataUp.Volunteer(aLong,true,null));
    }

    /**
     * 通过申请
     * @param aLong
     * @param id
     */
    @Override
    @Transactional
    public void successVolunteer(Long id, Long aLong) {
        // 被审核用户id
        Long userId = volunteerMapper.queryUserId(id, aLong);
        if (userId != null) {
            volunteerMapper.updateVolunteerStatusYes(aLong,id);
            mechanismDataMapper.updateIn(MechanismDataUp.Volunteer(aLong,true,null));
            userFeiController.removeUserDetails(userId);
        }
    }

    /**
     * 拒绝申请
     * @param aLong
     * @param id
     */
    @Override
    public void ErrorVolunteer(Long id, Long aLong) {
        volunteerMapper.deleteVoluStatusNo(aLong,id);
    }
}
